<?php namespace Admin\Controller;
/**
 * 商品控制器
 */
class GoodsController extends CommonController
{
	protected $db;
	/**
	 * 构造方法
	 */
	public function __init()
	{
		$this->db = new \Common\Model\Goods;
	}
	/**
	 * 首页
	 */
	public function index()
	{
		//获取商品旧数据
		$goodsData = $this->db->get();
		//分配数据
		View::with('goodsData',$goodsData);
		View::make();
	}
	/**
	 * 添加
	 */
	public function store()
	{
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				$this->error($this->db->getError());
			}else{				

				$this->success('添加成功','index');
			}
		}
		//实例化分类模型
		$cateModel = new \Common\Model\Category;
		//获取分类旧数据
		$cateData = $cateModel->get();
		//获取品牌旧数据
		$brandData = Db::table('brand')->get();
		//分配数据
		View::with('brandData',$brandData);
		View::with('cateData',$cateData);
		View::make();
	}
	/**
	 * 修改
	 */
	public function edit()
	{
		$gid = Q('get.goods_id',0,'intval');
		if(IS_POST)
		{
			if(!$this->db->edit($gid))
			{
				$this->error($this->db->getError());
			}else{
				$this->success('修改成功',U('index'));
			}
		}
		
		//获得商品属性数据
		$goodsattrData = Db::table('goods_attr')
		                 ->join('typeattr','goodsattr_typeattr_id','=','typeattr_id')
						 ->where('goodsattr_goods_id',$gid)
						 ->where('typeattr_type',0)
						 ->get();
		foreach($goodsattrData as $k=>$v)
			{
				$value = explode('，', $v['typeattr_value']);
				$goodsattrData[$k]['typeattr_value'] = $value;
			}
		//获得商品规格属性数据
		$goodsspecData = Db::table('goods_attr')
		                 ->join('typeattr','goodsattr_typeattr_id','=','typeattr_id')
						 ->where('goodsattr_goods_id',$gid)
						 ->where('typeattr_type',1)
						 ->get();
		foreach($goodsspecData as $k=>$v)
			{
				$value = explode('，', $v['typeattr_value']);
				$goodsspecData[$k]['typeattr_value'] = $value;
			}
		//获取商品旧数据
		$goodsData = $this->db->where('goods_id',$gid)->first();
		//获取分类旧数据
		$cateData = Db::table('category')->get();
		//获取品牌旧数据
		$brandData = Db::table('brand')->get();
		//获取商品详情数据
		$detailData = Db::table('detail')->where('detail_goods_id',$gid)->first();
		$detailData['small'] = explode("|", $detailData['small']);
		$detailData['medium'] = explode("|", $detailData['medium']);
		$detailData['big'] = explode("|", $detailData['big']);
		
		//分配数据
		View::with('goodsspecData',$goodsspecData);
		View::with('goodsattrData',$goodsattrData);
		View::with('brandData',$brandData);
		View::with('goodsData',$goodsData);
		View::with('cateData',$cateData);
		View::with('detailData',$detailData);
		View::make();
	}
    /**
	 * 删除
	 */
	public function del()
	{
		$gid = Q('get.goods_id',0,'intval');
		$goodsData = new \Common\Model\Goods;
		$goodsData->where('goods_id',$gid)->delete();
		$goodsattrData = new \Common\Model\GoodsAttr;
		$goodsattrData->where('goodsattr_goods_id',$gid)->delete();
		$detailData = new \Common\Model\Detail;
		$detailData->where('detail_goods_id',$gid)->delete();
		$this->success('删除成功','index');
	}
	/**
	 * 异步调取规格和属性
	 */
	public function ajaxGetAtrr()
	{
		//获取异步传来的数据
		if(IS_AJAX)
		{
			$cid = $_POST['cid'];
		    $type_id = Db::table('category')->where('category_id',$cid)->pluck('category_type_id');
			$data = Db::table('typeattr')->where('typeattr_type_id',$type_id)->get();
			foreach($data as $k=>$v)
			{
				$value = explode('，', $v['typeattr_value']);
				$data[$k]['typeattr_value'] = $value;
			}
			echo json_encode($data);  			
		}
	}
	/**
	 * 异步上传
	 */
	public function ajaxUpload()
	{
		$files = Upload::path('Upload/goods' . date('ymd'))->make();
		if($files)
		{			
			View::ajax($files[0]);
		}
		View::ajax("上传错误");
	}
	/**
	 * 异步删除
	 */
	public function delImg()
	{
		if(IS_AJAX)
		{
			$path = $_POST['path'];
			if(unlink($path)){
				View::ajax(1);
			}
			View::ajax(0);
		}
	}
}
